<template>
  <div class="container" ref="mainCon">
    <topTitle :titleText="titleText" :titleExplain="titleExplain"></topTitle>
    <div class="mainCon">
      <el-row class="toolbar searchInputWidth" style="padding-bottom: 0">
        <el-form :inline="true" :model="filterField" ref="searchConditions">
          <el-col :lg="8" :xl="6">
            <el-form-item label="区域" class="row-padding-bottom">
              <el-cascader
                ref="manageArea"
                v-model="areaId"
                :options="areasOptions"
                @change="getAreaId"
                clearable
                filterable
                :props="{ checkStrictly: true }"
              ></el-cascader>
            </el-form-item>
          </el-col>
          <el-col :lg="8" :xl="6">
            <el-form-item label="状态" class="row-padding-bottom">
              <el-select
                v-model="filterField.status"
                filterable
                placeholder="请选择"
                clearable
              >
                <el-option
                  v-for="item in statusOptions"
                  :key="item.id"
                  :label="item.text"
                  :value="item.id"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <template v-if="advanced">
            <el-col :lg="8" :xl="6">
              <el-form-item label="申请日期" class="row-padding-bottom">
                <timeSelection
                  @getStartTimeValue="getStartTimeValue"
                  @getEndTimeValue="getEndTimeValue"
                ></timeSelection>
              </el-form-item>
            </el-col>
          </template>
          <el-form-item label="">
            <a
              @click="toggleAdvanced"
              style="margin-left: 40px; color: #409eff; cursor: pointer"
              class="btnColor"
            >
              {{ advanced ? '收起' : '展开' }}
              <i
                :class="advanced ? 'el-icon-arrow-up' : 'el-icon-arrow-down'"
              ></i>
            </a>
            <el-button
              type="primary"
              icon="icon iconfont iconchazhao1"
              @click="searchList"
              class="searchBtn"
            >
              <span>搜索</span>
            </el-button>
          </el-form-item>
        </el-form>
      </el-row>
      <div class="spacing"></div>
      <div
        ref="tableBox"
        :class="hiddleToolBar ? 'tableBoxCur tableBox' : 'tableBoxCur'"
      >
        <el-table
          ref="tableBox1"
          @sort-change="sortChange"
          :max-height="tableHeight"
          :data="tableList"
          :header-cell-style="{ background: '#eef1f6', color: '#606266' }"
          highlight-current-row
          v-loading="loading"
          stripe
          style="border: 1px solid #dfe6ec"
        >
          <el-table-column
            prop="areaName"
            label="所在区域"
            align="center"
            width="180"
            :formatter="formatTd"
          ></el-table-column>
          <el-table-column
            prop="bankName"
            label="银行"
            align="center"
            width="180"
            :formatter="formatTd"
          ></el-table-column>
          <el-table-column
            prop="bankSiteName"
            label="网点"
            align="center"
            width="180"
            :formatter="formatTd"
          ></el-table-column>
          <el-table-column
            prop="agentName"
            label="经办人"
            align="center"
            width="160"
            :formatter="formatTd"
          ></el-table-column>
          <el-table-column
            prop="phoneNum"
            label="联系电话"
            align="center"
            width="160"
            :formatter="formatTd"
          ></el-table-column>
          <el-table-column
            prop="createTime"
            sortable="custom"
            label="申报时间"
            align="center"
            width="160"
            :formatter="formatTd"
          ></el-table-column>
          <el-table-column
            prop="statusDesc"
            label="状态"
            align="center"
            width="100"
            :formatter="formatTd"
          ></el-table-column>
          <el-table-column label="操作" width="160" align="left" fixed="right">
            <template slot-scope="scope">
              <el-link
                v-hasBtn="'bookAccount:update'"
                @click="showDialog(scope.row, 'edit')"
                v-if="
                  scope.row.status === 'WAIT_AUDIT' &&
                  $store.state.userType === 'BANK'
                "
                type="primary"
              >
                审核
              </el-link>
              <el-link
                v-hasBtn="'bookAccount:detail'"
                v-else
                @click="showDialog(scope.row, 'detail')"
                type="primary"
              >
                查看详情
              </el-link>
            </template>
          </el-table-column>
          <!-- 表格数据为空时，页面显示 -->
          <div slot="empty" v-show="isShow">
            <emptyTable emptyText="暂无相关信息"></emptyTable>
          </div>
        </el-table>
        <Pagination
          ref="pagination"
          @getList="getTableList"
          :filterField="filterField"
          :requestUrl="requestUrl"
          @getpageNo="getpageNo"
          @getpageSize="getpageSize"
        ></Pagination>
      </div>
    </div>

    <!-- 审核 -->
    <el-dialog
      :title="type === 'edit' ? '审核' : '查看详情'"
      :visible.sync="examineDialog"
      @close="closeDialog"
      :close-on-click-modal="false"
      :modal-append-to-body="false"
      width="900px"
    >
      <el-form
        :model="formData"
        :rules="rules"
        ref="ruleForm"
        label-width="120px"
        style="overflow-y: auto"
      >
        <div class="secondaryTitle secondaryTitleTheme mrB30 mrT4">
          基础信息
        </div>
        <el-row>
          <el-col :span="12" class="labelWidthSty mrB30">
            <span class="gridContent color333 fontSizeF fontWeight"
              >所在区域：</span
            >
            <span class="gridContent contentSty color666 fontSizeF">{{
              accountDetail.areaName || '--'
            }}</span>
          </el-col>
          <el-col :span="12" class="labelWidthSty mrB30">
            <span class="gridContent color333 fontSizeF fontWeight"
              >银行：</span
            >
            <span class="gridContent contentSty color666 fontSizeF">{{
              accountDetail.bankName || '--'
            }}</span>
          </el-col>
          <el-col :span="12" class="labelWidthSty mrB30">
            <span class="gridContent color333 fontSizeF fontWeight"
              >网点：</span
            >
            <span class="gridContent contentSty color666 fontSizeF">{{
              accountDetail.bankSiteName || '--'
            }}</span>
          </el-col>
          <el-col :span="12" class="labelWidthSty mrB30">
            <span class="gridContent color333 fontSizeF fontWeight"
              >经办人：</span
            >
            <span class="gridContent contentSty color666 fontSizeF">{{
              accountDetail.agentName || '--'
            }}</span>
          </el-col>
          <el-col :span="12" class="labelWidthSty mrB30">
            <span class="gridContent color333 fontSizeF fontWeight"
              >联系电话：</span
            >
            <span class="gridContent contentSty color666 fontSizeF">{{
              accountDetail.phoneNum || '--'
            }}</span>
          </el-col>
          <el-col :span="12" class="labelWidthSty mrB30">
            <span class="gridContent color333 fontSizeF fontWeight"
              >施工合同：</span
            >
            <span class="gridContent contentSty color666 fontSizeF">
              <template
                v-if="
                  accountDetail.accountReserveFiles &&
                  accountDetail.accountReserveFiles.length > 0
                "
              >
                <span>已上传</span>
                <el-link
                  @click="showAttach(accountDetail.accountReserveFiles)"
                  style="
                    vertical-align: text-bottom;
                    line-height: 15px !important;
                  "
                  type="primary"
                  >查看</el-link
                >
              </template>
              <template v-else>
                <span>未上传</span>
              </template>
            </span>
          </el-col>
          <el-col :span="24" class="labelWidthSty mrB30">
            <span class="gridContent color333 fontSizeF fontWeight"
              >备注：</span
            >
            <span class="gridContent contentSty color666 fontSizeF">{{
              accountDetail.remark || '--'
            }}</span>
          </el-col>
        </el-row>
        <el-divider></el-divider>
        <div class="secondaryTitle secondaryTitleTheme mrB30 mrT24">
          预约处理
        </div>
        <tempalte v-if="type === 'edit'">
          <el-form-item label="处理方式：">
            <el-radio-group v-model="formData.status">
              <el-radio label="WAIT_AUDIT">变更网点</el-radio>
              <el-radio label="PASSED">审核通过</el-radio>
              <el-radio label="REJECTED">驳回</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item
            :label="formData.status === 'PASSED' ? '预约网点：' : '选择网点：'"
            prop="bankSiteId"
            class="row-padding-bottom"
            v-if="
              formData.status == 'PASSED' || formData.status == 'WAIT_AUDIT'
            "
          >
            <el-select
              v-model="formData.bankSiteId"
              placeholder="请选择网点"
              :disabled="formData.status == 'PASSED'"
              clearable
              filterable
              class="dialogDoubleInp"
            >
              <el-option
                v-for="(item, index) of bankBranchOptions"
                :key="index"
                :label="item.name"
                :value="item.code"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item
            label="预约时间："
            prop="reviewTime"
            class="row-padding-bottom"
            v-if="formData.status === 'PASSED'"
          >
            <el-date-picker
              type="datetime"
              value-format="yyyy-MM-dd HH:mm:ss"
              class="caseCompTitleInput dialogDoubleInp"
              placeholder="请选择时间"
              v-model="formData.reviewTime"
            ></el-date-picker>
          </el-form-item>
          <el-form-item
            :label="formData.status === 'PASSED' ? '预约说明：' : '驳回说明：'"
            prop="description"
            v-if="
              formData.status === 'PASSED' || formData.status === 'REJECTED'
            "
          >
            <el-input
              v-model="formData.description"
              type="textarea"
              maxlength="300"
              :rows="3"
              show-word-limit
              :placeholder="
                formData.status === 'PASSED'
                  ? '请输入预约说明'
                  : '请输入驳回原因'
              "
              clearable
            ></el-input>
          </el-form-item>
        </tempalte>
        <template v-if="type === 'detail'">
          <el-col :span="12" class="labelWidthSty mrB30">
            <span class="gridContent color333 fontSizeF fontWeight"
              >处理方式：</span
            >
            <span class="gridContent contentSty color666 fontSizeF">{{
              accountDetail.statusDesc || '--'
            }}</span>
          </el-col>
          <el-col
            :span="12"
            class="labelWidthSty mrB30"
            v-if="
              accountDetail.status === 'WAIT_AUDIT' ||
              accountDetail.status === 'PASSED'
            "
          >
            <span class="gridContent color333 fontSizeF fontWeight">{{
              accountDetail.status === 'PASSED' ? '预约网点：' : '选择网点：'
            }}</span>
            <span class="gridContent contentSty color666 fontSizeF">{{
              accountDetail.bankSiteName || '--'
            }}</span>
          </el-col>
          <el-col
            :span="12"
            class="labelWidthSty mrB30"
            v-if="accountDetail.status === 'PASSED'"
          >
            <span class="gridContent color333 fontSizeF fontWeight"
              >预约时间：</span
            >
            <span class="gridContent contentSty color666 fontSizeF">{{
              accountDetail.reviewTime || '--'
            }}</span>
          </el-col>
          <el-col
            :span="12"
            class="labelWidthSty mrB30"
            v-if="
              accountDetail.status === 'REJECTED' ||
              accountDetail.status === 'PASSED'
            "
          >
            <span class="gridContent color333 fontSizeF fontWeight">{{
              accountDetail.status === 'PASSED' ? '预约说明：' : '驳回说明：'
            }}</span>
            <span class="gridContent contentSty color666 fontSizeF">{{
              accountDetail.description || '--'
            }}</span>
          </el-col>
        </template>
      </el-form>
      <div div slot="footer" v-if="type === 'edit'" class="dialog-footer">
        <el-button @click.native="closeDialog" class="cancel">取消</el-button>
        <el-button
          type="primary"
          @click.native="submitForm"
          v-sdc
          :loading="isLoading"
          class="save"
          >保存</el-button
        >
      </div>
    </el-dialog>
    <!-- 附件 -->
    <contractEnclosureDialog
      v-if="uploadContract"
      dialogTitle="竣工报告"
      :tableData="fileBackList"
      @closeDialog="hiddenDialog"
    ></contractEnclosureDialog>
  </div>
</template>
<script>
import { mapState } from 'vuex'
import scrollTable from '@/mixin/scrollTable.js'
export default {
  name: 'bookAccount',
  mixins: [scrollTable],
  data() {
    return {
      titleText: '专户开设预约',
      titleExplain: '对于符合开设专户条件的企业，预约开设时间和准备资料事宜',
      statusOptions: [
        {
          text: '待审核',
          id: 'WAIT_AUDIT',
        },
        {
          text: '已通过',
          id: 'PASSED',
        },
        {
          text: '已驳回',
          id: 'REJECTED',
        },
      ],
      tableList: [],
      requestUrl: 'api-acc/accountreserve/expand/list',
      areaId: '', //区域
      filterField: {
        areaId: '', //区域
        status: '', //开工状态
        startTime: '',
        endTime: '',
      },
      pageSize: 20,
      pageNo: 1,
      loading: false,
      isShow: false,
      examineDialog: false, //弹窗
      type: '', //审核/详情
      accountDetail: {}, //专户详情
      formData: {
        id: '',
        bankSiteId: '',
        status: 'WAIT_AUDIT',
        reviewTime: '',
        description: '',
      },
      rules: {
        bankSiteId: [
          {
            required: true,
            message: '请选择网点',
            trigger: 'change',
          },
        ],
        reviewTime: [
          {
            required: true,
            message: '请选择时间',
            trigger: 'change',
          },
        ],
        description: [
          {
            required: true,
            message: '请输入说明',
            trigger: 'blur',
          },
        ],
      },
      isLoading: false,
      bankBranchOptions: [], //网点列表
      uploadContract: false, //附件
      fileBackList: [],
      advanced: false,
    }
  },
  computed: {
    ...mapState(['areasOptions', 'industryOptions']),
  },
  created() {
    if (this.$route.query.status) {
      this.filterField.status = this.$route.query.status
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.$refs.pagination.getTableListData()
    })
  },
  watch: {
    $route(to, from) {
      if (this.$route.query.status) {
        this.filterField.status = this.$route.query.status
      }
      this.$nextTick(() => {
        this.$refs.pagination.getTableListData()
      })
    },
  },
  methods: {
    toggleAdvanced() {
      this.advanced = !this.advanced
    },
    getStartTimeValue(val) {
      if (val) {
        this.filterField.startTime = val
      } else {
        this.filterField.startTime = ''
      }
    },
    getEndTimeValue(val) {
      if (val) {
        this.filterField.endTime = val
      } else {
        this.filterField.endTime = ''
      }
    },
    getpageSize(val) {
      this.pageSize = val
    },
    getpageNo(val) {
      this.pageNo = val
    },
    // 列表
    getTableList(data) {
      this.tableList = data
      this.loading = false
      if (this.tableList.length < 1) {
        this.isShow = true
      }
    },
    searchList() {
      this.$refs.pagination.getTableListData()
    },
    //获取选中的区域最后一级id
    getAreaId() {
      var areaId = this.areaId[this.areaId.length - 1]
      this.filterField.areaId = areaId
    },
    // 弹窗
    showDialog(row, type) {
      this.accountDetail = row
      this.formData.id = row.id
      this.formData.bankSiteId = row.bankSiteId
      this.getAccountreserveDetail(row.id)
      this.type = type
      this.examineDialog = true
      this.getBankBranchList(row.bankId)
    },
    // 查询详情
    getAccountreserveDetail(id) {
      return this.axios({
        method: 'post',
        url: 'api-acc/accountreserve/expand/detail',
        data: { id },
      })
        .then((response) => {
          if (!response.data.hasError) {
            this.accountDetail.accountReserveFiles =
              response.data.result.accountReserveFiles
          } else {
            this.$message.error(response.data.errorMsg)
          }
        })
        .catch((error) => {
          if (error.response) {
            this.$message.error(error.response.data)
          }
        })
    },
    closeDialog() {
      this.$refs['ruleForm'].resetFields()
      this.formData.status = 'WAIT_AUDIT'
      this.examineDialog = false
    },
    //保存
    submitForm() {
      this.$refs['ruleForm'].validate((valid) => {
        if (valid) {
          this.isLoading = true
          const { id, status, bankSiteId, reviewTime, description } =
            this.formData
          let params = { id, status, bankSiteId, reviewTime, description }
          this.axios
            .post('api-acc/accountreserve/expand/review', params)
            .then((res) => {
              if (!res.data.hasError) {
                this.$message.success('保存成功')
                this.isLoading = false
                this.formData.id = ''
                this.formData.bankSiteId = ''
                this.formData.status = 'WAIT_AUDIT'
                this.formData.reviewTime = ''
                this.formData.description = ''
                this.examineDialog = false
                this.$refs.pagination.getTableListData(
                  this.pageNo,
                  this.pageSize
                )
              } else {
                this.$message({
                  type: 'warning',
                  message: res.data.errorMsg,
                })
                this.isLoading = false
              }
            })
        } else {
          return false
        }
      })
    },
    showAttach(files) {
      this.fileBackList = files
      this.uploadContract = true
    },
    hiddenDialog() {
      this.uploadContract = false
    },
    // 获取银行网点
    getBankBranchList(bankId) {
      this.axios
        .post('api-acc/accountbank/expand/banksite', { bankId })
        .then((res) => {
          this.bankBranchOptions = res.data.result
        })
        .catch((error) => {
          this.$message({
            message: '获取网点数据失败',
            type: 'error',
          })
        })
    },
  },
}
</script>
<style lang="less" scoped>
.mainCon {
  top: 210px;
}
</style>
